<?php
$adm_menu = "1";

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") header('Location: index.php');


$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);



if (isset($_REQUEST["gr"])) {
	$gid = $_REQUEST["gr"];
} else {
	$gid = "";
}
if (isset($_REQUEST["findim"])){
		$test_q = "SELECT N19_KODAS_GS,N19_PAV FROM [n19_pgr] WHERE [N19_KODAS_GS] = '".addslashes_mssql($gid)."'";
		
		$im = $db_www->returnInfo($test_q);	
		if ($im == null) {
			$err = "Nerasta prekių grupė tokiu kodu!";
			$grpav = "";			
		} else {
			$grpav = iconv('windows-1257','UTF-8', trim($im["N19_PAV"]));			
		}
}
if (!isset($grpav)){
	if (isset($_REQUEST["grpav"])) {
		$grpav = $_REQUEST["grpav"];
	} else {
		$grpav = "";
	}
} 
	if (isset($_REQUEST["dydis"])) {
		$dydis = $_REQUEST["dydis"];
	} else {
		$dydis = "";
	}

if (isset($_REQUEST["tipas"])){
	$tipas = $_REQUEST["tipas"];
} else {
	$tipas = "1";
}


if (isset($_REQUEST["act"])){
	if ($_REQUEST["act"] == "del" && isset($_REQUEST["id"])) {
		$ndel_query = str_replace("<id>", addslashes_mssql($_REQUEST["id"]), $ndel_query);
		$db_rdata->execute($ndel_query);		
	} else if ($_REQUEST["act"] == "save" && !isset($_REQUEST["findim"])) {
		$test_q = "SELECT N19_KODAS_GS,N19_PAV FROM [n19_pgr] WHERE [N19_KODAS_GS] = '".addslashes_mssql($gid)."'";
		
		$err = "";
		$im = $db_www->returnInfo($test_q);	
		if ($im == null) {
			$err .= "Nerasta prekių grupė tokiu kodu!";			
		} else {
			$grpav = iconv('windows-1257','UTF-8', trim($im["N19_PAV"]));			
		}
		if (isset($_REQUEST["tipas"])){
			if ($_REQUEST["tipas"] == "2"){
				if (is_numeric($_REQUEST["dydis"])){
					if ($_REQUEST["dydis"] < 0 || $_REQUEST["dydis"] > 100) {
						$err .= "<br>Neteisinga nuolaidų dydžio reikšmė!";
					}
				} else {
					$err .= "<br>Neteisinga nuolaidų dydžio reikšmė!";
				}
			} else {
				if (is_numeric($_REQUEST["dydis"])){
					if ($_REQUEST["dydis"] < 0) {
						$err .= "<br>Neteisinga nuolaidų dydžio reikšmė!";
					}
				} else {
					$err .= "<br>Neteisinga nuolaidų dydžio reikšmė!";
				}
			}
		} else {
			$err .= "<br>Neteisinga nuolaidų tipo reikšmė!";	
		}
		
		if ($err == "") {
			$sql = "exec web_insert_nuol "
			. "@N08_n_kodas_ks='".addslashes_mssql($_REQUEST["code"])."',"
			. "@N08_n_kodas_gs='".addslashes_mssql($_REQUEST["gr"])."',"
			. "@N08_n_tipas='".addslashes_mssql($_REQUEST["tipas"])."',"
			. "@N08_n_dydis='".addslashes_mssql($_REQUEST["dydis"])."'";
			
			$db_rdata->execute($sql);
			$gid = "";
			$tipas = "1";
			$dydis = "";
			$grpav = "";
		}
		
	}
}
	
include("header.php");
include("menu.php");



	if (isset($_REQUEST["code"]) && $_REQUEST["code"] != "0") {
		$code = $_REQUEST["code"];
		$uid = $code;		
		if (isset($_REQUEST["uid"])) {
			$sql = str_replace("<kodas>", addslashes_mssql($_REQUEST["uid"]), $uinfo_query);
		} else {
			$sql = str_replace("<kodas>", addslashes_mssql($uid), $uinfo_query);
		}
		
		$u = $db_www->returnInfo($sql);
		if ($u == null) die("nerastas vartotojas");
		$imcode = trim($u["n60_kodas_ks"]);	

	} else {
		die("nerastas vartotojas");
	}

			
?>


<table class="innercont" cellspacing="0" cellpadding="10" border="0">
<tr><td class="leftcol2">




</td><td class="rightcol" colspan="2">


<table width="100%" border="0">

<tr>
<td valign="top">
<script  type="text/javascript">
	$(document).ready(function(){	

		if (!$.browser.opera) {
    
			// select element styling
			$('select.select').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		
	});
</script>

</td>
</tr>

<tr><td valign="top">
<?php
if (isset($err) && $err != ""){
	echo "<p style='background-color:transparent;color:red;font-weight:bold;font-size:10px;'>".$err."</p>";
}
?>
<script>
function delDiscount(id){
	var answer = confirm("Ar ištrinti nuolaidą?");
	if (answer){
		window.location='edit_discounts.php?code=<?php echo $code;?>&act=del&id='+id ;
	}

}
</script>
<form method="post" name='forma' action='edit_discounts.php?act=save'>
<input id="code" type="hidden" name="code" value="<?php echo $code?>">
<table cellspacing="0" cellpadding="5" align="left" class="innercont3">
<tr>
<th>Prekių grupė</th><th>Grupės pavadinimas</th><th>Nuolaidos tipas</th><th>Nuolaidos dydis</th><th></th></tr>
<?php
$sql = "SELECT n.* FROM n08_klij_n n WHERE n.N08_n_kodas_ks='".addslashes_mssql($code)."'";

$list = $db_rdata->returnList($sql);
foreach ($list as $item) {
	echo "<tr>";
	echo "<td>".htmlspecialchars($item["N08_n_kodas_gs"])."</td>";
	echo "<td>".htmlspecialchars(getNuolGrPav($item["N08_n_kodas_gs"]))."</td>";
	echo "<td>";
	if ($item["N08_n_tipas"] == 1) {
		echo "Suma vienetui";
	} else {
		echo "Procentai";
	}
	
	echo "</td>";
	echo "<td>".htmlspecialchars($item["N08_n_dydis"])."</td>";
	echo "<td class='click' onclick=\"delDiscount('".$item["N08_n_id"]."');return false;\"><img src='img/delete.jpg' width='11' alt='Trinti' title='Trinti'></td>";
	echo "</tr>";
}

?>
<tr><td><input class="inp" type='text' name="gr" style="width:80px;" value="<?php echo htmlspecialchars($gid); ?>"><br>
<div style="float:right;"><a class="flink" href="#" onclick='document.forma.action="edit_discounts.php?act=save&findim=1";document.forma.submit()'>Ieškoti</a></div>
</td>
<td valign="top"><input name="grpav" class="inp" type='text' value="<?php echo htmlspecialchars($grpav); ?>" readonly></td>
<td valign="top">
<div style="position: relative;">
<select name="tipas" class="select">
<option value="1" <?php if ($tipas == "1") echo "SELECTED";?>>Suma vienetui</option>
<option value="2" <?php if ($tipas == "2") echo "SELECTED";?>>Procentai</option>
</select>
</div>
</td>
<td valign="top"><input class="inp" type='text' name="dydis" style="width:80px;" value="<?php echo htmlspecialchars($dydis); ?>"></td>
<td valign="top"><input class="but bgwhite nopad" type="submit" value="Išsaugoti"></td></tr>
</table>
</form>
</td></tr>
</table>



</td></tr>

<tr><td>&nbsp;</td><td>

<Button id='unblockButton' class='but bgwhite nopad' onclick="window.location='<?php echo "adiscounts.php?code=".$_REQUEST["code"];?>';return false;">Atgal</Button>

<?php

?>
</td>
<td style="text-align:right;">

</td>
</tr>
</table>

<?php
include("footer.php");
?>